﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineManagement.Web.webs
{
    public partial class SowAdd :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Expires = 0;
            //Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            //Response.AddHeader("progma", "no-cache");
            //Response.AddHeader("cache-control", "private");
            //Response.CacheControl = "no-cache";

            btnSave.Click+=new EventHandler(btnSave_Click);
            if(!Page.IsPostBack){
                txtHoggeryName.Text = HoggeryName;
                BindDropDownLists(HoggeryId);
            }
        }
         

        void btnSave_Click(object sender, EventArgs e)
        {
            string bitId = txtBitId.Text.Trim();
            string dateJoinStr = txtDateIncome.Text;
            DateTime dateJoin;
            DateTime dateBirthday;
            Guid pigPenId;
            Guid pigTypeId;
            Guid propagateStateID;
            int sourceId;
            int hybridizationCount = 0;
            int propagateCount = 0;
            if (HoggeryId == 0 || string.IsNullOrEmpty(txtHoggeryName.Text))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, Common.PubMsg.NoOnlineMgrData);
                return;
            }
            if (string.IsNullOrEmpty(bitId))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, Common.PubMsg.SowBitIDISEmpty);
                return;
            }
            if (!Guid.TryParse(ddlPigPen.SelectedItem.Value, out pigPenId))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "猪舍号不能为空.");
                return;
            }
            if (!DateTime.TryParse(dateJoinStr, out dateJoin))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "进栏日期项不能为空.");
                return;
            }
            if (!DateTime.TryParse(txtBirthday.Text, out dateBirthday))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "出生日期项不能为空.");
                return;
            }
            
            if (!Guid.TryParse(ddlPigType.SelectedItem.Value, out pigTypeId))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "母猪类型不能为空.");
                return;
            }
            if (!int.TryParse(ddlSource.SelectedItem.Value, out sourceId))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "母猪来源不能为空.");
                return;
            }
            if (!Guid.TryParse(ddlPropagateState.SelectedItem.Value, out propagateStateID))
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.attention, "母猪繁殖状态不能为空.");
                return;
            }
            int.TryParse(txthybridizationCount.Text.Trim(), out hybridizationCount);
            int.TryParse(txtPropagateCount.Text.Trim(), out propagateCount);
            Model.Sow sow = new Model.Sow();
            sow.HoggeryID = HoggeryId;
            sow.BitID = bitId;
            sow.PigPenID = pigPenId;
            sow.TypeID = pigTypeId;
            sow.SourceID = sourceId;
            sow.DateJoinFarm = dateJoin;
            sow.PropagateStateID = propagateStateID;
            sow.Brithdary = dateBirthday;
            sow.HybridizationCount = hybridizationCount;
            sow.PropagateCount = propagateCount;

            string retureValue = string.Empty;
            BLL.Sow bllSow = new BLL.Sow();
            string sowIdStr;
            try
            {
                bool result = bllSow.Add(sow, out retureValue, out sowIdStr);

                log4net.ILog log = log4net.LogManager.GetLogger(new System.Diagnostics.StackFrame(0).GetMethod().Name);
                Com.HoggeryLog businessLog = new Com.HoggeryLog()
                {
                    HoggeryID = HoggeryId,
                    PigId = sowIdStr,
                    BusinessLogDate = System.DateTime.Now,
                    LogType = OnlineManagement.Web.Com.HoggeryLogType.SowAdd,
                };

                if (result)
                {
                    businessLog.Message = string.Format("新加{0}号母猪", bitId);
                    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.success, retureValue);
                }
                else
                {
                    Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, retureValue);
                }
                businessLog.LogComment = retureValue;
                log.Info(businessLog);
                ClearControlValue();

            }
            catch (Exception ex)
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, Common.PubMsg.ServerError);
            }
        }


        private void BindDropDownLists(int hoggeryId)
        {
            try
            {
                BLL.Sow bllSow = new BLL.Sow();
                System.Data.DataSet ds = bllSow.GetSowSettingsInfo(hoggeryId);
                if (ds != null && ds.Tables.Count > 0)
                {
                    ddlPigPen.DataSource = ds.Tables[0];
                    ddlPigPen.DataTextField = "Name";
                    ddlPigPen.DataValueField = "ID";
                    ddlPigPen.DataBind();
                    ListItem item = new ListItem("--选择猪圈号--");
                    ddlPigPen.Items.Insert(0, item);

                    ddlPigType.DataSource = ds.Tables[1];
                    ddlPigType.DataTextField = "Name";
                    ddlPigType.DataValueField = "ID";
                    ddlPigType.DataBind();

                    ddlSource.DataSource = ds.Tables[2];
                    ddlSource.DataTextField = "Name";
                    ddlSource.DataValueField = "ID";
                    ddlSource.DataBind();

                    ddlPropagateState.DataSource = ds.Tables[3];
                    ddlPropagateState.DataTextField = "Name";
                    ddlPropagateState.DataValueField = "ID";
                    ddlPropagateState.DataBind();
                }

            }
            catch (Exception ex)
            {
                Common.MessageBox.PrintMsg(panMsg, Common.MsgLevelEnum.error, ex.Message);
            }
        }
        private void ClearControlValue()
        {
            txtBitId.Text = string.Empty;
        }
    }
}